{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 关联规则\n",
    "\n",
    "使用 mlxtend 工具包得出频繁项集与规则"
   ],
   "id": "b477d0111d6878c1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T10:18:43.965393Z",
     "start_time": "2025-04-21T10:18:43.300071Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori       # 01弃用，bool启用\n",
    "from mlxtend.frequent_patterns import association_rules"
   ],
   "id": "bfd85d95e7dfaffc",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "自定义一份购物数据集",
   "id": "be1459e2d4ad21f3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T10:26:57.632837Z",
     "start_time": "2025-04-21T10:26:57.623754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    'Onion': [1, 0, 0, 1, 1, 1], \n",
    "    'Potato': [1, 1, 0, 1, 1, 1], \n",
    "    'Burger': [1, 1, 0, 0, 1, 1], \n",
    "    'Milk': [0, 1, 1, 1, 0, 1], \n",
    "    'Beer': [0, 0, 1, 0, 1, 0] \n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "df"
   ],
   "id": "f1b33160f635b006",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Onion  Potato  Burger  Milk  Beer\n",
       "0      1       1       1     0     0\n",
       "1      0       1       1     1     0\n",
       "2      0       0       0     1     1\n",
       "3      1       1       0     1     0\n",
       "4      1       1       1     0     1\n",
       "5      1       1       1     1     0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Onion</th>\n",
       "      <th>Potato</th>\n",
       "      <th>Burger</th>\n",
       "      <th>Milk</th>\n",
       "      <th>Beer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 设置 支持度(support) 来选择频繁项集\n",
    "\n",
    "选择 最小支持度 为 50% (min_support=0.5)"
   ],
   "id": "86e21ce477c45f55"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T10:29:09.504941Z",
     "start_time": "2025-04-21T10:29:09.494115Z"
    }
   },
   "cell_type": "code",
   "source": [
    "frequent_item_sets = apriori(df.astype(bool), min_support=0.5, use_colnames=True)\n",
    "frequent_item_sets"
   ],
   "id": "487abdea062132f9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    support                 itemsets\n",
       "0  0.666667                  (Onion)\n",
       "1  0.833333                 (Potato)\n",
       "2  0.666667                 (Burger)\n",
       "3  0.666667                   (Milk)\n",
       "4  0.666667          (Onion, Potato)\n",
       "5  0.500000          (Onion, Burger)\n",
       "6  0.666667         (Burger, Potato)\n",
       "7  0.500000           (Milk, Potato)\n",
       "8  0.500000  (Onion, Burger, Potato)"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Onion)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.833333</td>\n",
       "      <td>(Potato)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Burger)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Milk)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Onion, Potato)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>(Onion, Burger)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Burger, Potato)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>(Milk, Potato)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>(Onion, Burger, Potato)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "返回的三种项集均是支持度 >= 50%",
   "id": "bd96717707835324"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 计算规则\n",
    "\n",
    "可以指定不同的 衡量标准 和 最小阈值"
   ],
   "id": "dbd4a2557896a1f1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T10:33:31.435401Z",
     "start_time": "2025-04-21T10:33:31.413092Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rules = association_rules(frequent_item_sets, metric='lift', min_threshold=1)\n",
    "rules"
   ],
   "id": "d2345e7180e125c7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         antecedents       consequents  antecedent support  \\\n",
       "0            (Onion)          (Potato)            0.666667   \n",
       "1           (Potato)           (Onion)            0.833333   \n",
       "2            (Onion)          (Burger)            0.666667   \n",
       "3           (Burger)           (Onion)            0.666667   \n",
       "4           (Burger)          (Potato)            0.666667   \n",
       "5           (Potato)          (Burger)            0.833333   \n",
       "6    (Onion, Burger)          (Potato)            0.500000   \n",
       "7    (Onion, Potato)          (Burger)            0.666667   \n",
       "8   (Burger, Potato)           (Onion)            0.666667   \n",
       "9            (Onion)  (Burger, Potato)            0.666667   \n",
       "10          (Burger)   (Onion, Potato)            0.666667   \n",
       "11          (Potato)   (Onion, Burger)            0.833333   \n",
       "\n",
       "    consequent support   support  confidence   lift  representativity  \\\n",
       "0             0.833333  0.666667        1.00  1.200               1.0   \n",
       "1             0.666667  0.666667        0.80  1.200               1.0   \n",
       "2             0.666667  0.500000        0.75  1.125               1.0   \n",
       "3             0.666667  0.500000        0.75  1.125               1.0   \n",
       "4             0.833333  0.666667        1.00  1.200               1.0   \n",
       "5             0.666667  0.666667        0.80  1.200               1.0   \n",
       "6             0.833333  0.500000        1.00  1.200               1.0   \n",
       "7             0.666667  0.500000        0.75  1.125               1.0   \n",
       "8             0.666667  0.500000        0.75  1.125               1.0   \n",
       "9             0.666667  0.500000        0.75  1.125               1.0   \n",
       "10            0.666667  0.500000        0.75  1.125               1.0   \n",
       "11            0.500000  0.500000        0.60  1.200               1.0   \n",
       "\n",
       "    leverage  conviction  zhangs_metric  jaccard  certainty  kulczynski  \n",
       "0   0.111111         inf       0.500000      0.8       1.00        0.90  \n",
       "1   0.111111    1.666667       1.000000      0.8       0.40        0.90  \n",
       "2   0.055556    1.333333       0.333333      0.6       0.25        0.75  \n",
       "3   0.055556    1.333333       0.333333      0.6       0.25        0.75  \n",
       "4   0.111111         inf       0.500000      0.8       1.00        0.90  \n",
       "5   0.111111    1.666667       1.000000      0.8       0.40        0.90  \n",
       "6   0.083333         inf       0.333333      0.6       1.00        0.80  \n",
       "7   0.055556    1.333333       0.333333      0.6       0.25        0.75  \n",
       "8   0.055556    1.333333       0.333333      0.6       0.25        0.75  \n",
       "9   0.055556    1.333333       0.333333      0.6       0.25        0.75  \n",
       "10  0.055556    1.333333       0.333333      0.6       0.25        0.75  \n",
       "11  0.083333    1.250000       1.000000      0.6       0.20        0.80  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>representativity</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "      <th>jaccard</th>\n",
       "      <th>certainty</th>\n",
       "      <th>kulczynski</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(Onion)</td>\n",
       "      <td>(Potato)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(Potato)</td>\n",
       "      <td>(Onion)</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.80</td>\n",
       "      <td>1.200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>1.666667</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(Onion)</td>\n",
       "      <td>(Burger)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(Burger)</td>\n",
       "      <td>(Onion)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(Burger)</td>\n",
       "      <td>(Potato)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(Potato)</td>\n",
       "      <td>(Burger)</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.80</td>\n",
       "      <td>1.200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>1.666667</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(Onion, Burger)</td>\n",
       "      <td>(Potato)</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(Onion, Potato)</td>\n",
       "      <td>(Burger)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(Burger, Potato)</td>\n",
       "      <td>(Onion)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(Onion)</td>\n",
       "      <td>(Burger, Potato)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(Burger)</td>\n",
       "      <td>(Onion, Potato)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(Potato)</td>\n",
       "      <td>(Onion, Burger)</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.60</td>\n",
       "      <td>1.200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>1.250000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "返回的是各个指标值，可以按照感兴趣的指标排序观察，但具体解释还得参考实际数据的含义。",
   "id": "a3b888b2e6f64924"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T10:40:30.886276Z",
     "start_time": "2025-04-21T10:40:30.871009Z"
    }
   },
   "cell_type": "code",
   "source": "rules[(rules['lift'] > 1.125) & (rules['confidence'] > 0.8)]",
   "id": "7a2bbcdcc2dab504",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "       antecedents consequents  antecedent support  consequent support  \\\n",
       "0          (Onion)    (Potato)            0.666667            0.833333   \n",
       "4         (Burger)    (Potato)            0.666667            0.833333   \n",
       "6  (Onion, Burger)    (Potato)            0.500000            0.833333   \n",
       "\n",
       "    support  confidence  lift  representativity  leverage  conviction  \\\n",
       "0  0.666667         1.0   1.2               1.0  0.111111         inf   \n",
       "4  0.666667         1.0   1.2               1.0  0.111111         inf   \n",
       "6  0.500000         1.0   1.2               1.0  0.083333         inf   \n",
       "\n",
       "   zhangs_metric  jaccard  certainty  kulczynski  \n",
       "0       0.500000      0.8        1.0         0.9  \n",
       "4       0.500000      0.8        1.0         0.9  \n",
       "6       0.333333      0.6        1.0         0.8  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>representativity</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "      <th>jaccard</th>\n",
       "      <th>certainty</th>\n",
       "      <th>kulczynski</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(Onion)</td>\n",
       "      <td>(Potato)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(Burger)</td>\n",
       "      <td>(Potato)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.8</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(Onion, Burger)</td>\n",
       "      <td>(Potato)</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "1. **(洋葱 和 马铃薯) (汉堡 和 马铃薯) 可以搭配着售卖**\n",
    "\n",
    "2. **如果 洋葱 和 马铃薯 都在购物篮里，顾客购买马铃薯的可能性就比较高，如果他篮子里没有，可以推荐一下。**"
   ],
   "id": "c3d31b6605a84ce0"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 所有指标的计算公式：\n",
    "\n",
    "![](./Correlation/Calculation_formula.png)"
   ],
   "id": "55f9a105f4201ce"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数据需转换成 One-Hot 独热编码",
   "id": "24c9f03183905935"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T11:10:52.275668Z",
     "start_time": "2025-04-21T11:10:52.267936Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# json 格式\n",
    "retail_shopping_basket = {\n",
    "    'ID': list(range(1, 7)), \n",
    "    'Basket': [\n",
    "        ['Beer', 'Diaper', 'Pretzels', 'Chips', 'Aspirin'], \n",
    "        ['Diaper', 'Beer', 'Chips', 'Lotion', 'Juice', 'BabyFood', 'Milk'], \n",
    "        ['Soda', 'Chips', 'Milk'], \n",
    "        ['Soup', 'Beer', 'Diaper', 'Milk', 'IceCream'], \n",
    "        ['Soda', 'Coffee', 'Milk', 'Bread'], \n",
    "        ['Beer', 'Chips']\n",
    "    ]\n",
    "}\n",
    "retail = pd.DataFrame(retail_shopping_basket)\n",
    "retail"
   ],
   "id": "3b8b3ac73ccf84ad",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   ID                                             Basket\n",
       "0   1           [Beer, Diaper, Pretzels, Chips, Aspirin]\n",
       "1   2  [Diaper, Beer, Chips, Lotion, Juice, BabyFood,...\n",
       "2   3                                [Soda, Chips, Milk]\n",
       "3   4               [Soup, Beer, Diaper, Milk, IceCream]\n",
       "4   5                        [Soda, Coffee, Milk, Bread]\n",
       "5   6                                      [Beer, Chips]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Basket</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>[Beer, Diaper, Pretzels, Chips, Aspirin]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>[Diaper, Beer, Chips, Lotion, Juice, BabyFood,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>[Soda, Chips, Milk]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>[Soup, Beer, Diaper, Milk, IceCream]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>[Soda, Coffee, Milk, Bread]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>[Beer, Chips]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "数据集中都是字符串组成的，需要转换成编码",
   "id": "362498055cc75064"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T11:08:32.552085Z",
     "start_time": "2025-04-21T11:08:32.543317Z"
    }
   },
   "cell_type": "code",
   "source": [
    "retail_id = retail.drop('Basket', axis=1)\n",
    "retail_id"
   ],
   "id": "bc9356ced87d03d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   ID\n",
       "0   1\n",
       "1   2\n",
       "2   3\n",
       "3   4\n",
       "4   5\n",
       "5   6"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T11:29:21.018204Z",
     "start_time": "2025-04-21T11:29:21.003567Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# .str.join(',') 是 Pandas 的字符串操作方法，专门处理 Series 中每个元素的字符串/列表。\n",
    "# 类似 Python 中 ','.join([\"Beer\", \"Diaper\"]) 的效果\n",
    "retail_basket = retail['Basket'].str.join(',')\n",
    "# 类似 Python 中 ','.get_dummies('Beer,Diaper,Pretzels,Chips,Aspirin') 的效果\n",
    "retail_basket = retail_basket.str.get_dummies(',')\n",
    "retail_basket"
   ],
   "id": "7847ae3741ea78b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   Aspirin  BabyFood  Beer  Bread  Chips  Coffee  Diaper  IceCream  Juice  \\\n",
       "0        1         0     1      0      1       0       1         0      0   \n",
       "1        0         1     1      0      1       0       1         0      1   \n",
       "2        0         0     0      0      1       0       0         0      0   \n",
       "3        0         0     1      0      0       0       1         1      0   \n",
       "4        0         0     0      1      0       1       0         0      0   \n",
       "5        0         0     1      0      1       0       0         0      0   \n",
       "\n",
       "   Lotion  Milk  Pretzels  Soda  Soup  \n",
       "0       0     0         1     0     0  \n",
       "1       1     1         0     0     0  \n",
       "2       0     1         0     1     0  \n",
       "3       0     1         0     0     1  \n",
       "4       0     1         0     1     0  \n",
       "5       0     0         0     0     0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Aspirin</th>\n",
       "      <th>BabyFood</th>\n",
       "      <th>Beer</th>\n",
       "      <th>Bread</th>\n",
       "      <th>Chips</th>\n",
       "      <th>Coffee</th>\n",
       "      <th>Diaper</th>\n",
       "      <th>IceCream</th>\n",
       "      <th>Juice</th>\n",
       "      <th>Lotion</th>\n",
       "      <th>Milk</th>\n",
       "      <th>Pretzels</th>\n",
       "      <th>Soda</th>\n",
       "      <th>Soup</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T11:32:02.915075Z",
     "start_time": "2025-04-21T11:32:02.900080Z"
    }
   },
   "cell_type": "code",
   "source": [
    "frequent_item_sets2 = apriori(retail_basket.astype(bool), use_colnames=True)\n",
    "frequent_item_sets2"
   ],
   "id": "7b2b5bdd2779e602",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    support        itemsets\n",
       "0  0.666667          (Beer)\n",
       "1  0.666667         (Chips)\n",
       "2  0.500000        (Diaper)\n",
       "3  0.666667          (Milk)\n",
       "4  0.500000   (Chips, Beer)\n",
       "5  0.500000  (Beer, Diaper)"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Beer)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Chips)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>(Diaper)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.666667</td>\n",
       "      <td>(Milk)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>(Chips, Beer)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>(Beer, Diaper)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "如果只考虑 支持度support(X==>Y), (Beer, Diaper) 和 (Chips, Beer) 都是很频繁的，哪一种组合更相关呢？",
   "id": "371bd49a738c60b6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:03:40.931934Z",
     "start_time": "2025-04-21T12:03:40.915497Z"
    }
   },
   "cell_type": "code",
   "source": "association_rules(frequent_item_sets2, metric='lift')",
   "id": "6729a2b737a28bc1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  antecedents consequents  antecedent support  consequent support  support  \\\n",
       "0     (Chips)      (Beer)            0.666667            0.666667      0.5   \n",
       "1      (Beer)     (Chips)            0.666667            0.666667      0.5   \n",
       "2      (Beer)    (Diaper)            0.666667            0.500000      0.5   \n",
       "3    (Diaper)      (Beer)            0.500000            0.666667      0.5   \n",
       "\n",
       "   confidence   lift  representativity  leverage  conviction  zhangs_metric  \\\n",
       "0        0.75  1.125               1.0  0.055556    1.333333       0.333333   \n",
       "1        0.75  1.125               1.0  0.055556    1.333333       0.333333   \n",
       "2        0.75  1.500               1.0  0.166667    2.000000       1.000000   \n",
       "3        1.00  1.500               1.0  0.166667         inf       0.666667   \n",
       "\n",
       "   jaccard  certainty  kulczynski  \n",
       "0     0.60       0.25       0.750  \n",
       "1     0.60       0.25       0.750  \n",
       "2     0.75       0.50       0.875  \n",
       "3     0.75       1.00       0.875  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>representativity</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "      <th>jaccard</th>\n",
       "      <th>certainty</th>\n",
       "      <th>kulczynski</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(Chips)</td>\n",
       "      <td>(Beer)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(Beer)</td>\n",
       "      <td>(Chips)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.125</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(Beer)</td>\n",
       "      <td>(Diaper)</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.500</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(Diaper)</td>\n",
       "      <td>(Beer)</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.500</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.875</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "显然 (Beer, Diaper) 更相关一些",
   "id": "136d74d973a4ed2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 电影题材关联\n",
    "\n",
    "数据集：[MovieLens (small)](https://grouplens.org/datasets/movielens/)"
   ],
   "id": "6e5d9325c0258221"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:37:04.184097Z",
     "start_time": "2025-04-21T12:37:04.165072Z"
    }
   },
   "cell_type": "code",
   "source": [
    "movies = pd.read_csv('./Correlation/movies.csv')\n",
    "movies.head(10)"
   ],
   "id": "db8bbbf4807e488c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   movieId                               title  \\\n",
       "0        1                    Toy Story (1995)   \n",
       "1        2                      Jumanji (1995)   \n",
       "2        3             Grumpier Old Men (1995)   \n",
       "3        4            Waiting to Exhale (1995)   \n",
       "4        5  Father of the Bride Part II (1995)   \n",
       "5        6                         Heat (1995)   \n",
       "6        7                      Sabrina (1995)   \n",
       "7        8                 Tom and Huck (1995)   \n",
       "8        9                 Sudden Death (1995)   \n",
       "9       10                    GoldenEye (1995)   \n",
       "\n",
       "                                        genres  \n",
       "0  Adventure|Animation|Children|Comedy|Fantasy  \n",
       "1                   Adventure|Children|Fantasy  \n",
       "2                               Comedy|Romance  \n",
       "3                         Comedy|Drama|Romance  \n",
       "4                                       Comedy  \n",
       "5                        Action|Crime|Thriller  \n",
       "6                               Comedy|Romance  \n",
       "7                           Adventure|Children  \n",
       "8                                       Action  \n",
       "9                    Action|Adventure|Thriller  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movieId</th>\n",
       "      <th>title</th>\n",
       "      <th>genres</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Toy Story (1995)</td>\n",
       "      <td>Adventure|Animation|Children|Comedy|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jumanji (1995)</td>\n",
       "      <td>Adventure|Children|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Grumpier Old Men (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Waiting to Exhale (1995)</td>\n",
       "      <td>Comedy|Drama|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Father of the Bride Part II (1995)</td>\n",
       "      <td>Comedy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>Heat (1995)</td>\n",
       "      <td>Action|Crime|Thriller</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>Sabrina (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>Tom and Huck (1995)</td>\n",
       "      <td>Adventure|Children</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>Sudden Death (1995)</td>\n",
       "      <td>Action</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>GoldenEye (1995)</td>\n",
       "      <td>Action|Adventure|Thriller</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "数据中包括电影名称与电影类型，先转换成 One-Hot 独热编码。",
   "id": "8276bd4f65ba1c65"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:37:17.961583Z",
     "start_time": "2025-04-21T12:37:17.909287Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.concat([movies[['movieId', 'title']], movies['genres'].str.get_dummies('|')], axis=1)\n",
    "data"
   ],
   "id": "8f7c6e0dc60b6f30",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      movieId                                              title  \\\n",
       "0           1                                   Toy Story (1995)   \n",
       "1           2                                     Jumanji (1995)   \n",
       "2           3                            Grumpier Old Men (1995)   \n",
       "3           4                           Waiting to Exhale (1995)   \n",
       "4           5                 Father of the Bride Part II (1995)   \n",
       "...       ...                                                ...   \n",
       "9120   162672                                Mohenjo Daro (2016)   \n",
       "9121   163056                               Shin Godzilla (2016)   \n",
       "9122   163949  The Beatles: Eight Days a Week - The Touring Y...   \n",
       "9123   164977                           The Gay Desperado (1936)   \n",
       "9124   164979                              Women of '69, Unboxed   \n",
       "\n",
       "      (no genres listed)  Action  Adventure  Animation  Children  Comedy  \\\n",
       "0                      0       0          1          1         1       1   \n",
       "1                      0       0          1          0         1       0   \n",
       "2                      0       0          0          0         0       1   \n",
       "3                      0       0          0          0         0       1   \n",
       "4                      0       0          0          0         0       1   \n",
       "...                  ...     ...        ...        ...       ...     ...   \n",
       "9120                   0       0          1          0         0       0   \n",
       "9121                   0       1          1          0         0       0   \n",
       "9122                   0       0          0          0         0       0   \n",
       "9123                   0       0          0          0         0       1   \n",
       "9124                   0       0          0          0         0       0   \n",
       "\n",
       "      Crime  Documentary  ...  Film-Noir  Horror  IMAX  Musical  Mystery  \\\n",
       "0         0            0  ...          0       0     0        0        0   \n",
       "1         0            0  ...          0       0     0        0        0   \n",
       "2         0            0  ...          0       0     0        0        0   \n",
       "3         0            0  ...          0       0     0        0        0   \n",
       "4         0            0  ...          0       0     0        0        0   \n",
       "...     ...          ...  ...        ...     ...   ...      ...      ...   \n",
       "9120      0            0  ...          0       0     0        0        0   \n",
       "9121      0            0  ...          0       0     0        0        0   \n",
       "9122      0            1  ...          0       0     0        0        0   \n",
       "9123      0            0  ...          0       0     0        0        0   \n",
       "9124      0            1  ...          0       0     0        0        0   \n",
       "\n",
       "      Romance  Sci-Fi  Thriller  War  Western  \n",
       "0           0       0         0    0        0  \n",
       "1           0       0         0    0        0  \n",
       "2           1       0         0    0        0  \n",
       "3           1       0         0    0        0  \n",
       "4           0       0         0    0        0  \n",
       "...       ...     ...       ...  ...      ...  \n",
       "9120        1       0         0    0        0  \n",
       "9121        0       1         0    0        0  \n",
       "9122        0       0         0    0        0  \n",
       "9123        0       0         0    0        0  \n",
       "9124        0       0         0    0        0  \n",
       "\n",
       "[9125 rows x 22 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movieId</th>\n",
       "      <th>title</th>\n",
       "      <th>(no genres listed)</th>\n",
       "      <th>Action</th>\n",
       "      <th>Adventure</th>\n",
       "      <th>Animation</th>\n",
       "      <th>Children</th>\n",
       "      <th>Comedy</th>\n",
       "      <th>Crime</th>\n",
       "      <th>Documentary</th>\n",
       "      <th>...</th>\n",
       "      <th>Film-Noir</th>\n",
       "      <th>Horror</th>\n",
       "      <th>IMAX</th>\n",
       "      <th>Musical</th>\n",
       "      <th>Mystery</th>\n",
       "      <th>Romance</th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>Thriller</th>\n",
       "      <th>War</th>\n",
       "      <th>Western</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Toy Story (1995)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jumanji (1995)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Grumpier Old Men (1995)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Waiting to Exhale (1995)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Father of the Bride Part II (1995)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9120</th>\n",
       "      <td>162672</td>\n",
       "      <td>Mohenjo Daro (2016)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9121</th>\n",
       "      <td>163056</td>\n",
       "      <td>Shin Godzilla (2016)</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9122</th>\n",
       "      <td>163949</td>\n",
       "      <td>The Beatles: Eight Days a Week - The Touring Y...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9123</th>\n",
       "      <td>164977</td>\n",
       "      <td>The Gay Desperado (1936)</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9124</th>\n",
       "      <td>164979</td>\n",
       "      <td>Women of '69, Unboxed</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9125 rows × 22 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:37:31.946620Z",
     "start_time": "2025-04-21T12:37:31.940367Z"
    }
   },
   "cell_type": "code",
   "source": "data.shape",
   "id": "2dee65877ea19bb5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9125, 22)"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:39:06.539114Z",
     "start_time": "2025-04-21T12:39:06.517361Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data.set_index(['movieId', 'title'], inplace=True)\n",
    "data.head()"
   ],
   "id": "325f0a48e07d6506",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                                            (no genres listed)  Action  \\\n",
       "movieId title                                                            \n",
       "1       Toy Story (1995)                                     0       0   \n",
       "2       Jumanji (1995)                                       0       0   \n",
       "3       Grumpier Old Men (1995)                              0       0   \n",
       "4       Waiting to Exhale (1995)                             0       0   \n",
       "5       Father of the Bride Part II (1995)                   0       0   \n",
       "\n",
       "                                            Adventure  Animation  Children  \\\n",
       "movieId title                                                                \n",
       "1       Toy Story (1995)                            1          1         1   \n",
       "2       Jumanji (1995)                              1          0         1   \n",
       "3       Grumpier Old Men (1995)                     0          0         0   \n",
       "4       Waiting to Exhale (1995)                    0          0         0   \n",
       "5       Father of the Bride Part II (1995)          0          0         0   \n",
       "\n",
       "                                            Comedy  Crime  Documentary  Drama  \\\n",
       "movieId title                                                                   \n",
       "1       Toy Story (1995)                         1      0            0      0   \n",
       "2       Jumanji (1995)                           0      0            0      0   \n",
       "3       Grumpier Old Men (1995)                  1      0            0      0   \n",
       "4       Waiting to Exhale (1995)                 1      0            0      1   \n",
       "5       Father of the Bride Part II (1995)       1      0            0      0   \n",
       "\n",
       "                                            Fantasy  Film-Noir  Horror  IMAX  \\\n",
       "movieId title                                                                  \n",
       "1       Toy Story (1995)                          1          0       0     0   \n",
       "2       Jumanji (1995)                            1          0       0     0   \n",
       "3       Grumpier Old Men (1995)                   0          0       0     0   \n",
       "4       Waiting to Exhale (1995)                  0          0       0     0   \n",
       "5       Father of the Bride Part II (1995)        0          0       0     0   \n",
       "\n",
       "                                            Musical  Mystery  Romance  Sci-Fi  \\\n",
       "movieId title                                                                   \n",
       "1       Toy Story (1995)                          0        0        0       0   \n",
       "2       Jumanji (1995)                            0        0        0       0   \n",
       "3       Grumpier Old Men (1995)                   0        0        1       0   \n",
       "4       Waiting to Exhale (1995)                  0        0        1       0   \n",
       "5       Father of the Bride Part II (1995)        0        0        0       0   \n",
       "\n",
       "                                            Thriller  War  Western  \n",
       "movieId title                                                       \n",
       "1       Toy Story (1995)                           0    0        0  \n",
       "2       Jumanji (1995)                             0    0        0  \n",
       "3       Grumpier Old Men (1995)                    0    0        0  \n",
       "4       Waiting to Exhale (1995)                   0    0        0  \n",
       "5       Father of the Bride Part II (1995)         0    0        0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>(no genres listed)</th>\n",
       "      <th>Action</th>\n",
       "      <th>Adventure</th>\n",
       "      <th>Animation</th>\n",
       "      <th>Children</th>\n",
       "      <th>Comedy</th>\n",
       "      <th>Crime</th>\n",
       "      <th>Documentary</th>\n",
       "      <th>Drama</th>\n",
       "      <th>Fantasy</th>\n",
       "      <th>Film-Noir</th>\n",
       "      <th>Horror</th>\n",
       "      <th>IMAX</th>\n",
       "      <th>Musical</th>\n",
       "      <th>Mystery</th>\n",
       "      <th>Romance</th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>Thriller</th>\n",
       "      <th>War</th>\n",
       "      <th>Western</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movieId</th>\n",
       "      <th>title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>Toy Story (1995)</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <th>Jumanji (1995)</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <th>Grumpier Old Men (1995)</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <th>Waiting to Exhale (1995)</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <th>Father of the Bride Part II (1995)</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:41:18.876348Z",
     "start_time": "2025-04-21T12:41:18.854372Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 类型多，数据量大，min_support 较小\n",
    "frequent_item_sets_movies = apriori(data.astype(bool), min_support=0.025, use_colnames=True)\n",
    "frequent_item_sets_movies"
   ],
   "id": "c900b182a9adbf06",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     support                  itemsets\n",
       "0   0.169315                  (Action)\n",
       "1   0.122411               (Adventure)\n",
       "2   0.048986               (Animation)\n",
       "3   0.063890                (Children)\n",
       "4   0.363288                  (Comedy)\n",
       "5   0.120548                   (Crime)\n",
       "6   0.054247             (Documentary)\n",
       "7   0.478356                   (Drama)\n",
       "8   0.071671                 (Fantasy)\n",
       "9   0.096110                  (Horror)\n",
       "10  0.043178                 (Musical)\n",
       "11  0.059507                 (Mystery)\n",
       "12  0.169315                 (Romance)\n",
       "13  0.086795                  (Sci-Fi)\n",
       "14  0.189479                (Thriller)\n",
       "15  0.040219                     (War)\n",
       "16  0.058301       (Action, Adventure)\n",
       "17  0.037589          (Comedy, Action)\n",
       "18  0.038247           (Action, Crime)\n",
       "19  0.051178           (Drama, Action)\n",
       "20  0.040986          (Sci-Fi, Action)\n",
       "21  0.062904        (Thriller, Action)\n",
       "22  0.029260     (Children, Adventure)\n",
       "23  0.036712       (Comedy, Adventure)\n",
       "24  0.032438        (Drama, Adventure)\n",
       "25  0.030685      (Adventure, Fantasy)\n",
       "26  0.027726       (Sci-Fi, Adventure)\n",
       "27  0.027068     (Children, Animation)\n",
       "28  0.032877        (Children, Comedy)\n",
       "29  0.032438           (Comedy, Crime)\n",
       "30  0.104000           (Drama, Comedy)\n",
       "31  0.026959         (Comedy, Fantasy)\n",
       "32  0.090082         (Comedy, Romance)\n",
       "33  0.067616            (Drama, Crime)\n",
       "34  0.057863         (Thriller, Crime)\n",
       "35  0.031671          (Drama, Mystery)\n",
       "36  0.101260          (Drama, Romance)\n",
       "37  0.087123         (Thriller, Drama)\n",
       "38  0.031014              (Drama, War)\n",
       "39  0.043397        (Thriller, Horror)\n",
       "40  0.036055       (Thriller, Mystery)\n",
       "41  0.028932        (Sci-Fi, Thriller)\n",
       "42  0.035068  (Drama, Romance, Comedy)\n",
       "43  0.032000  (Thriller, Drama, Crime)"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.169315</td>\n",
       "      <td>(Action)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.122411</td>\n",
       "      <td>(Adventure)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.048986</td>\n",
       "      <td>(Animation)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.063890</td>\n",
       "      <td>(Children)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.363288</td>\n",
       "      <td>(Comedy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.120548</td>\n",
       "      <td>(Crime)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.054247</td>\n",
       "      <td>(Documentary)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.478356</td>\n",
       "      <td>(Drama)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.071671</td>\n",
       "      <td>(Fantasy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.096110</td>\n",
       "      <td>(Horror)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.043178</td>\n",
       "      <td>(Musical)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.059507</td>\n",
       "      <td>(Mystery)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.169315</td>\n",
       "      <td>(Romance)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.086795</td>\n",
       "      <td>(Sci-Fi)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.189479</td>\n",
       "      <td>(Thriller)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.040219</td>\n",
       "      <td>(War)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.058301</td>\n",
       "      <td>(Action, Adventure)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.037589</td>\n",
       "      <td>(Comedy, Action)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.038247</td>\n",
       "      <td>(Action, Crime)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.051178</td>\n",
       "      <td>(Drama, Action)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.040986</td>\n",
       "      <td>(Sci-Fi, Action)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0.062904</td>\n",
       "      <td>(Thriller, Action)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.029260</td>\n",
       "      <td>(Children, Adventure)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0.036712</td>\n",
       "      <td>(Comedy, Adventure)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.032438</td>\n",
       "      <td>(Drama, Adventure)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.030685</td>\n",
       "      <td>(Adventure, Fantasy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.027726</td>\n",
       "      <td>(Sci-Fi, Adventure)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0.027068</td>\n",
       "      <td>(Children, Animation)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.032877</td>\n",
       "      <td>(Children, Comedy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.032438</td>\n",
       "      <td>(Comedy, Crime)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>0.104000</td>\n",
       "      <td>(Drama, Comedy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0.026959</td>\n",
       "      <td>(Comedy, Fantasy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.090082</td>\n",
       "      <td>(Comedy, Romance)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>0.067616</td>\n",
       "      <td>(Drama, Crime)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.057863</td>\n",
       "      <td>(Thriller, Crime)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>0.031671</td>\n",
       "      <td>(Drama, Mystery)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>0.101260</td>\n",
       "      <td>(Drama, Romance)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>0.087123</td>\n",
       "      <td>(Thriller, Drama)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>0.031014</td>\n",
       "      <td>(Drama, War)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>0.043397</td>\n",
       "      <td>(Thriller, Horror)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>0.036055</td>\n",
       "      <td>(Thriller, Mystery)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>0.028932</td>\n",
       "      <td>(Sci-Fi, Thriller)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>0.035068</td>\n",
       "      <td>(Drama, Romance, Comedy)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>0.032000</td>\n",
       "      <td>(Thriller, Drama, Crime)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:44:59.846245Z",
     "start_time": "2025-04-21T12:44:59.817214Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rules_movies = association_rules(frequent_item_sets_movies, metric='lift', min_threshold=1.25)\n",
    "rules_movies"
   ],
   "id": "129565c546c06dc3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          antecedents        consequents  antecedent support  \\\n",
       "0            (Action)        (Adventure)            0.169315   \n",
       "1         (Adventure)           (Action)            0.122411   \n",
       "2            (Action)            (Crime)            0.169315   \n",
       "3             (Crime)           (Action)            0.120548   \n",
       "4            (Sci-Fi)           (Action)            0.086795   \n",
       "5            (Action)           (Sci-Fi)            0.169315   \n",
       "6          (Thriller)           (Action)            0.189479   \n",
       "7            (Action)         (Thriller)            0.169315   \n",
       "8          (Children)        (Adventure)            0.063890   \n",
       "9         (Adventure)         (Children)            0.122411   \n",
       "10        (Adventure)          (Fantasy)            0.122411   \n",
       "11          (Fantasy)        (Adventure)            0.071671   \n",
       "12           (Sci-Fi)        (Adventure)            0.086795   \n",
       "13        (Adventure)           (Sci-Fi)            0.122411   \n",
       "14         (Children)        (Animation)            0.063890   \n",
       "15        (Animation)         (Children)            0.048986   \n",
       "16         (Children)           (Comedy)            0.063890   \n",
       "17           (Comedy)         (Children)            0.363288   \n",
       "18           (Comedy)          (Romance)            0.363288   \n",
       "19          (Romance)           (Comedy)            0.169315   \n",
       "20         (Thriller)            (Crime)            0.189479   \n",
       "21            (Crime)         (Thriller)            0.120548   \n",
       "22            (Drama)          (Romance)            0.478356   \n",
       "23          (Romance)            (Drama)            0.169315   \n",
       "24            (Drama)              (War)            0.478356   \n",
       "25              (War)            (Drama)            0.040219   \n",
       "26         (Thriller)           (Horror)            0.189479   \n",
       "27           (Horror)         (Thriller)            0.096110   \n",
       "28         (Thriller)          (Mystery)            0.189479   \n",
       "29          (Mystery)         (Thriller)            0.059507   \n",
       "30           (Sci-Fi)         (Thriller)            0.086795   \n",
       "31         (Thriller)           (Sci-Fi)            0.189479   \n",
       "32    (Drama, Comedy)          (Romance)            0.104000   \n",
       "33          (Romance)    (Drama, Comedy)            0.169315   \n",
       "34  (Thriller, Drama)            (Crime)            0.087123   \n",
       "35     (Drama, Crime)         (Thriller)            0.067616   \n",
       "36         (Thriller)     (Drama, Crime)            0.189479   \n",
       "37            (Crime)  (Thriller, Drama)            0.120548   \n",
       "\n",
       "    consequent support   support  confidence      lift  representativity  \\\n",
       "0             0.122411  0.058301    0.344337  2.812955               1.0   \n",
       "1             0.169315  0.058301    0.476276  2.812955               1.0   \n",
       "2             0.120548  0.038247    0.225890  1.873860               1.0   \n",
       "3             0.169315  0.038247    0.317273  1.873860               1.0   \n",
       "4             0.169315  0.040986    0.472222  2.789015               1.0   \n",
       "5             0.086795  0.040986    0.242071  2.789015               1.0   \n",
       "6             0.169315  0.062904    0.331984  1.960746               1.0   \n",
       "7             0.189479  0.062904    0.371521  1.960746               1.0   \n",
       "8             0.122411  0.029260    0.457976  3.741299               1.0   \n",
       "9             0.063890  0.029260    0.239033  3.741299               1.0   \n",
       "10            0.071671  0.030685    0.250671  3.497518               1.0   \n",
       "11            0.122411  0.030685    0.428135  3.497518               1.0   \n",
       "12            0.122411  0.027726    0.319444  2.609607               1.0   \n",
       "13            0.086795  0.027726    0.226500  2.609607               1.0   \n",
       "14            0.048986  0.027068    0.423671  8.648758               1.0   \n",
       "15            0.063890  0.027068    0.552573  8.648758               1.0   \n",
       "16            0.363288  0.032877    0.514580  1.416453               1.0   \n",
       "17            0.063890  0.032877    0.090498  1.416453               1.0   \n",
       "18            0.169315  0.090082    0.247964  1.464511               1.0   \n",
       "19            0.363288  0.090082    0.532039  1.464511               1.0   \n",
       "20            0.120548  0.057863    0.305379  2.533256               1.0   \n",
       "21            0.189479  0.057863    0.480000  2.533256               1.0   \n",
       "22            0.169315  0.101260    0.211684  1.250236               1.0   \n",
       "23            0.478356  0.101260    0.598058  1.250236               1.0   \n",
       "24            0.040219  0.031014    0.064834  1.612015               1.0   \n",
       "25            0.478356  0.031014    0.771117  1.612015               1.0   \n",
       "26            0.096110  0.043397    0.229034  2.383052               1.0   \n",
       "27            0.189479  0.043397    0.451539  2.383052               1.0   \n",
       "28            0.059507  0.036055    0.190283  3.197672               1.0   \n",
       "29            0.189479  0.036055    0.605893  3.197672               1.0   \n",
       "30            0.189479  0.028932    0.333333  1.759206               1.0   \n",
       "31            0.086795  0.028932    0.152689  1.759206               1.0   \n",
       "32            0.169315  0.035068    0.337197  1.991536               1.0   \n",
       "33            0.104000  0.035068    0.207120  1.991536               1.0   \n",
       "34            0.120548  0.032000    0.367296  3.046884               1.0   \n",
       "35            0.189479  0.032000    0.473258  2.497673               1.0   \n",
       "36            0.067616  0.032000    0.168884  2.497673               1.0   \n",
       "37            0.087123  0.032000    0.265455  3.046884               1.0   \n",
       "\n",
       "    leverage  conviction  zhangs_metric   jaccard  certainty  kulczynski  \n",
       "0   0.037575    1.338475       0.775868  0.249765   0.252881    0.410306  \n",
       "1   0.037575    1.586111       0.734401  0.249765   0.369527    0.410306  \n",
       "2   0.017836    1.136081       0.561395  0.152003   0.119781    0.271581  \n",
       "3   0.017836    1.216716       0.530264  0.152003   0.178115    0.271581  \n",
       "4   0.026291    1.573929       0.702416  0.190525   0.364647    0.357147  \n",
       "5   0.026291    1.204870       0.772195  0.190525   0.170035    0.357147  \n",
       "6   0.030822    1.243510       0.604537  0.212593   0.195825    0.351752  \n",
       "7   0.030822    1.289654       0.589863  0.212593   0.224598    0.351752  \n",
       "8   0.021439    1.619096       0.782722  0.186322   0.382371    0.348505  \n",
       "9   0.021439    1.230158       0.834916  0.186322   0.187096    0.348505  \n",
       "10  0.021912    1.238881       0.813687  0.187793   0.192820    0.339403  \n",
       "11  0.021912    1.534608       0.769213  0.187793   0.348368    0.339403  \n",
       "12  0.017101    1.289519       0.675424  0.152778   0.224517    0.272972  \n",
       "13  0.017101    1.180614       0.702835  0.152778   0.152983    0.272972  \n",
       "14  0.023939    1.650122       0.944736  0.315453   0.393984    0.488122  \n",
       "15  0.023939    2.092205       0.929930  0.315453   0.522035    0.488122  \n",
       "16  0.009666    1.311672       0.314077  0.083380   0.237615    0.302539  \n",
       "17  0.009666    1.029255       0.461764  0.083380   0.028423    0.302539  \n",
       "18  0.028572    1.104581       0.498150  0.203566   0.094679    0.390001  \n",
       "19  0.028572    1.360609       0.381827  0.203566   0.265035    0.390001  \n",
       "20  0.035022    1.266089       0.746744  0.229465   0.210166    0.392689  \n",
       "21  0.035022    1.558693       0.688214  0.229465   0.358437    0.392689  \n",
       "22  0.020267    1.053746       0.383693  0.185319   0.051005    0.404871  \n",
       "23  0.020267    1.297810       0.240947  0.185319   0.229471    0.404871  \n",
       "24  0.011775    1.026321       0.727811  0.063610   0.025646    0.417976  \n",
       "25  0.011775    2.279087       0.395568  0.063610   0.561228    0.417976  \n",
       "26  0.025186    1.172413       0.716046  0.179186   0.147058    0.340287  \n",
       "27  0.025186    1.477810       0.642080  0.179186   0.323323    0.340287  \n",
       "28  0.024779    1.161509       0.847940  0.169326   0.139051    0.398088  \n",
       "29  0.024779    2.056601       0.730758  0.169326   0.513761    0.398088  \n",
       "30  0.012486    1.215781       0.472579  0.116969   0.177483    0.243011  \n",
       "31  0.012486    1.077769       0.532450  0.116969   0.072158    0.243011  \n",
       "32  0.017460    1.253291       0.555664  0.147194   0.202101    0.272158  \n",
       "33  0.017460    1.130057       0.599355  0.147194   0.115089    0.272158  \n",
       "34  0.021497    1.389989       0.735911  0.182158   0.280570    0.316375  \n",
       "35  0.019188    1.538742       0.643112  0.142162   0.350119    0.321071  \n",
       "36  0.019188    1.121845       0.739805  0.142162   0.108611    0.321071  \n",
       "37  0.021497    1.242778       0.763880  0.182158   0.195351    0.316375  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>representativity</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "      <th>jaccard</th>\n",
       "      <th>certainty</th>\n",
       "      <th>kulczynski</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(Action)</td>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.058301</td>\n",
       "      <td>0.344337</td>\n",
       "      <td>2.812955</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.037575</td>\n",
       "      <td>1.338475</td>\n",
       "      <td>0.775868</td>\n",
       "      <td>0.249765</td>\n",
       "      <td>0.252881</td>\n",
       "      <td>0.410306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>(Action)</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.058301</td>\n",
       "      <td>0.476276</td>\n",
       "      <td>2.812955</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.037575</td>\n",
       "      <td>1.586111</td>\n",
       "      <td>0.734401</td>\n",
       "      <td>0.249765</td>\n",
       "      <td>0.369527</td>\n",
       "      <td>0.410306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(Action)</td>\n",
       "      <td>(Crime)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.038247</td>\n",
       "      <td>0.225890</td>\n",
       "      <td>1.873860</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.017836</td>\n",
       "      <td>1.136081</td>\n",
       "      <td>0.561395</td>\n",
       "      <td>0.152003</td>\n",
       "      <td>0.119781</td>\n",
       "      <td>0.271581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(Crime)</td>\n",
       "      <td>(Action)</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.038247</td>\n",
       "      <td>0.317273</td>\n",
       "      <td>1.873860</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.017836</td>\n",
       "      <td>1.216716</td>\n",
       "      <td>0.530264</td>\n",
       "      <td>0.152003</td>\n",
       "      <td>0.178115</td>\n",
       "      <td>0.271581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(Sci-Fi)</td>\n",
       "      <td>(Action)</td>\n",
       "      <td>0.086795</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.040986</td>\n",
       "      <td>0.472222</td>\n",
       "      <td>2.789015</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.026291</td>\n",
       "      <td>1.573929</td>\n",
       "      <td>0.702416</td>\n",
       "      <td>0.190525</td>\n",
       "      <td>0.364647</td>\n",
       "      <td>0.357147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(Action)</td>\n",
       "      <td>(Sci-Fi)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.086795</td>\n",
       "      <td>0.040986</td>\n",
       "      <td>0.242071</td>\n",
       "      <td>2.789015</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.026291</td>\n",
       "      <td>1.204870</td>\n",
       "      <td>0.772195</td>\n",
       "      <td>0.190525</td>\n",
       "      <td>0.170035</td>\n",
       "      <td>0.357147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Action)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.062904</td>\n",
       "      <td>0.331984</td>\n",
       "      <td>1.960746</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.030822</td>\n",
       "      <td>1.243510</td>\n",
       "      <td>0.604537</td>\n",
       "      <td>0.212593</td>\n",
       "      <td>0.195825</td>\n",
       "      <td>0.351752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(Action)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.062904</td>\n",
       "      <td>0.371521</td>\n",
       "      <td>1.960746</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.030822</td>\n",
       "      <td>1.289654</td>\n",
       "      <td>0.589863</td>\n",
       "      <td>0.212593</td>\n",
       "      <td>0.224598</td>\n",
       "      <td>0.351752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(Children)</td>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.029260</td>\n",
       "      <td>0.457976</td>\n",
       "      <td>3.741299</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021439</td>\n",
       "      <td>1.619096</td>\n",
       "      <td>0.782722</td>\n",
       "      <td>0.186322</td>\n",
       "      <td>0.382371</td>\n",
       "      <td>0.348505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>(Children)</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.029260</td>\n",
       "      <td>0.239033</td>\n",
       "      <td>3.741299</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021439</td>\n",
       "      <td>1.230158</td>\n",
       "      <td>0.834916</td>\n",
       "      <td>0.186322</td>\n",
       "      <td>0.187096</td>\n",
       "      <td>0.348505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>(Fantasy)</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.071671</td>\n",
       "      <td>0.030685</td>\n",
       "      <td>0.250671</td>\n",
       "      <td>3.497518</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021912</td>\n",
       "      <td>1.238881</td>\n",
       "      <td>0.813687</td>\n",
       "      <td>0.187793</td>\n",
       "      <td>0.192820</td>\n",
       "      <td>0.339403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(Fantasy)</td>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>0.071671</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.030685</td>\n",
       "      <td>0.428135</td>\n",
       "      <td>3.497518</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021912</td>\n",
       "      <td>1.534608</td>\n",
       "      <td>0.769213</td>\n",
       "      <td>0.187793</td>\n",
       "      <td>0.348368</td>\n",
       "      <td>0.339403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(Sci-Fi)</td>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>0.086795</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.027726</td>\n",
       "      <td>0.319444</td>\n",
       "      <td>2.609607</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.017101</td>\n",
       "      <td>1.289519</td>\n",
       "      <td>0.675424</td>\n",
       "      <td>0.152778</td>\n",
       "      <td>0.224517</td>\n",
       "      <td>0.272972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>(Sci-Fi)</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.086795</td>\n",
       "      <td>0.027726</td>\n",
       "      <td>0.226500</td>\n",
       "      <td>2.609607</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.017101</td>\n",
       "      <td>1.180614</td>\n",
       "      <td>0.702835</td>\n",
       "      <td>0.152778</td>\n",
       "      <td>0.152983</td>\n",
       "      <td>0.272972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>(Children)</td>\n",
       "      <td>(Animation)</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.048986</td>\n",
       "      <td>0.027068</td>\n",
       "      <td>0.423671</td>\n",
       "      <td>8.648758</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.023939</td>\n",
       "      <td>1.650122</td>\n",
       "      <td>0.944736</td>\n",
       "      <td>0.315453</td>\n",
       "      <td>0.393984</td>\n",
       "      <td>0.488122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>(Animation)</td>\n",
       "      <td>(Children)</td>\n",
       "      <td>0.048986</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.027068</td>\n",
       "      <td>0.552573</td>\n",
       "      <td>8.648758</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.023939</td>\n",
       "      <td>2.092205</td>\n",
       "      <td>0.929930</td>\n",
       "      <td>0.315453</td>\n",
       "      <td>0.522035</td>\n",
       "      <td>0.488122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>(Children)</td>\n",
       "      <td>(Comedy)</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.363288</td>\n",
       "      <td>0.032877</td>\n",
       "      <td>0.514580</td>\n",
       "      <td>1.416453</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.009666</td>\n",
       "      <td>1.311672</td>\n",
       "      <td>0.314077</td>\n",
       "      <td>0.083380</td>\n",
       "      <td>0.237615</td>\n",
       "      <td>0.302539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>(Comedy)</td>\n",
       "      <td>(Children)</td>\n",
       "      <td>0.363288</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.032877</td>\n",
       "      <td>0.090498</td>\n",
       "      <td>1.416453</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.009666</td>\n",
       "      <td>1.029255</td>\n",
       "      <td>0.461764</td>\n",
       "      <td>0.083380</td>\n",
       "      <td>0.028423</td>\n",
       "      <td>0.302539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>(Comedy)</td>\n",
       "      <td>(Romance)</td>\n",
       "      <td>0.363288</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.090082</td>\n",
       "      <td>0.247964</td>\n",
       "      <td>1.464511</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.028572</td>\n",
       "      <td>1.104581</td>\n",
       "      <td>0.498150</td>\n",
       "      <td>0.203566</td>\n",
       "      <td>0.094679</td>\n",
       "      <td>0.390001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>(Romance)</td>\n",
       "      <td>(Comedy)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.363288</td>\n",
       "      <td>0.090082</td>\n",
       "      <td>0.532039</td>\n",
       "      <td>1.464511</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.028572</td>\n",
       "      <td>1.360609</td>\n",
       "      <td>0.381827</td>\n",
       "      <td>0.203566</td>\n",
       "      <td>0.265035</td>\n",
       "      <td>0.390001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Crime)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.057863</td>\n",
       "      <td>0.305379</td>\n",
       "      <td>2.533256</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.035022</td>\n",
       "      <td>1.266089</td>\n",
       "      <td>0.746744</td>\n",
       "      <td>0.229465</td>\n",
       "      <td>0.210166</td>\n",
       "      <td>0.392689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>(Crime)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.057863</td>\n",
       "      <td>0.480000</td>\n",
       "      <td>2.533256</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.035022</td>\n",
       "      <td>1.558693</td>\n",
       "      <td>0.688214</td>\n",
       "      <td>0.229465</td>\n",
       "      <td>0.358437</td>\n",
       "      <td>0.392689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>(Drama)</td>\n",
       "      <td>(Romance)</td>\n",
       "      <td>0.478356</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.101260</td>\n",
       "      <td>0.211684</td>\n",
       "      <td>1.250236</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.020267</td>\n",
       "      <td>1.053746</td>\n",
       "      <td>0.383693</td>\n",
       "      <td>0.185319</td>\n",
       "      <td>0.051005</td>\n",
       "      <td>0.404871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>(Romance)</td>\n",
       "      <td>(Drama)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.478356</td>\n",
       "      <td>0.101260</td>\n",
       "      <td>0.598058</td>\n",
       "      <td>1.250236</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.020267</td>\n",
       "      <td>1.297810</td>\n",
       "      <td>0.240947</td>\n",
       "      <td>0.185319</td>\n",
       "      <td>0.229471</td>\n",
       "      <td>0.404871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>(Drama)</td>\n",
       "      <td>(War)</td>\n",
       "      <td>0.478356</td>\n",
       "      <td>0.040219</td>\n",
       "      <td>0.031014</td>\n",
       "      <td>0.064834</td>\n",
       "      <td>1.612015</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.011775</td>\n",
       "      <td>1.026321</td>\n",
       "      <td>0.727811</td>\n",
       "      <td>0.063610</td>\n",
       "      <td>0.025646</td>\n",
       "      <td>0.417976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>(War)</td>\n",
       "      <td>(Drama)</td>\n",
       "      <td>0.040219</td>\n",
       "      <td>0.478356</td>\n",
       "      <td>0.031014</td>\n",
       "      <td>0.771117</td>\n",
       "      <td>1.612015</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.011775</td>\n",
       "      <td>2.279087</td>\n",
       "      <td>0.395568</td>\n",
       "      <td>0.063610</td>\n",
       "      <td>0.561228</td>\n",
       "      <td>0.417976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Horror)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.096110</td>\n",
       "      <td>0.043397</td>\n",
       "      <td>0.229034</td>\n",
       "      <td>2.383052</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.025186</td>\n",
       "      <td>1.172413</td>\n",
       "      <td>0.716046</td>\n",
       "      <td>0.179186</td>\n",
       "      <td>0.147058</td>\n",
       "      <td>0.340287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>(Horror)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.096110</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.043397</td>\n",
       "      <td>0.451539</td>\n",
       "      <td>2.383052</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.025186</td>\n",
       "      <td>1.477810</td>\n",
       "      <td>0.642080</td>\n",
       "      <td>0.179186</td>\n",
       "      <td>0.323323</td>\n",
       "      <td>0.340287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Mystery)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.059507</td>\n",
       "      <td>0.036055</td>\n",
       "      <td>0.190283</td>\n",
       "      <td>3.197672</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.024779</td>\n",
       "      <td>1.161509</td>\n",
       "      <td>0.847940</td>\n",
       "      <td>0.169326</td>\n",
       "      <td>0.139051</td>\n",
       "      <td>0.398088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>(Mystery)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.059507</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.036055</td>\n",
       "      <td>0.605893</td>\n",
       "      <td>3.197672</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.024779</td>\n",
       "      <td>2.056601</td>\n",
       "      <td>0.730758</td>\n",
       "      <td>0.169326</td>\n",
       "      <td>0.513761</td>\n",
       "      <td>0.398088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>(Sci-Fi)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.086795</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.028932</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>1.759206</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.012486</td>\n",
       "      <td>1.215781</td>\n",
       "      <td>0.472579</td>\n",
       "      <td>0.116969</td>\n",
       "      <td>0.177483</td>\n",
       "      <td>0.243011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Sci-Fi)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.086795</td>\n",
       "      <td>0.028932</td>\n",
       "      <td>0.152689</td>\n",
       "      <td>1.759206</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.012486</td>\n",
       "      <td>1.077769</td>\n",
       "      <td>0.532450</td>\n",
       "      <td>0.116969</td>\n",
       "      <td>0.072158</td>\n",
       "      <td>0.243011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>(Drama, Comedy)</td>\n",
       "      <td>(Romance)</td>\n",
       "      <td>0.104000</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.035068</td>\n",
       "      <td>0.337197</td>\n",
       "      <td>1.991536</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.017460</td>\n",
       "      <td>1.253291</td>\n",
       "      <td>0.555664</td>\n",
       "      <td>0.147194</td>\n",
       "      <td>0.202101</td>\n",
       "      <td>0.272158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>(Romance)</td>\n",
       "      <td>(Drama, Comedy)</td>\n",
       "      <td>0.169315</td>\n",
       "      <td>0.104000</td>\n",
       "      <td>0.035068</td>\n",
       "      <td>0.207120</td>\n",
       "      <td>1.991536</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.017460</td>\n",
       "      <td>1.130057</td>\n",
       "      <td>0.599355</td>\n",
       "      <td>0.147194</td>\n",
       "      <td>0.115089</td>\n",
       "      <td>0.272158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>(Thriller, Drama)</td>\n",
       "      <td>(Crime)</td>\n",
       "      <td>0.087123</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.032000</td>\n",
       "      <td>0.367296</td>\n",
       "      <td>3.046884</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021497</td>\n",
       "      <td>1.389989</td>\n",
       "      <td>0.735911</td>\n",
       "      <td>0.182158</td>\n",
       "      <td>0.280570</td>\n",
       "      <td>0.316375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>(Drama, Crime)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.067616</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.032000</td>\n",
       "      <td>0.473258</td>\n",
       "      <td>2.497673</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.019188</td>\n",
       "      <td>1.538742</td>\n",
       "      <td>0.643112</td>\n",
       "      <td>0.142162</td>\n",
       "      <td>0.350119</td>\n",
       "      <td>0.321071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Drama, Crime)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.067616</td>\n",
       "      <td>0.032000</td>\n",
       "      <td>0.168884</td>\n",
       "      <td>2.497673</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.019188</td>\n",
       "      <td>1.121845</td>\n",
       "      <td>0.739805</td>\n",
       "      <td>0.142162</td>\n",
       "      <td>0.108611</td>\n",
       "      <td>0.321071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>(Crime)</td>\n",
       "      <td>(Thriller, Drama)</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.087123</td>\n",
       "      <td>0.032000</td>\n",
       "      <td>0.265455</td>\n",
       "      <td>3.046884</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021497</td>\n",
       "      <td>1.242778</td>\n",
       "      <td>0.763880</td>\n",
       "      <td>0.182158</td>\n",
       "      <td>0.195351</td>\n",
       "      <td>0.316375</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T12:48:00.323642Z",
     "start_time": "2025-04-21T12:48:00.308431Z"
    }
   },
   "cell_type": "code",
   "source": "rules_movies[(rules_movies['lift'] > 3)].sort_values('lift', ascending=False)",
   "id": "818163cfa258aab8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          antecedents        consequents  antecedent support  \\\n",
       "14         (Children)        (Animation)            0.063890   \n",
       "15        (Animation)         (Children)            0.048986   \n",
       "9         (Adventure)         (Children)            0.122411   \n",
       "8          (Children)        (Adventure)            0.063890   \n",
       "11          (Fantasy)        (Adventure)            0.071671   \n",
       "10        (Adventure)          (Fantasy)            0.122411   \n",
       "28         (Thriller)          (Mystery)            0.189479   \n",
       "29          (Mystery)         (Thriller)            0.059507   \n",
       "34  (Thriller, Drama)            (Crime)            0.087123   \n",
       "37            (Crime)  (Thriller, Drama)            0.120548   \n",
       "\n",
       "    consequent support   support  confidence      lift  representativity  \\\n",
       "14            0.048986  0.027068    0.423671  8.648758               1.0   \n",
       "15            0.063890  0.027068    0.552573  8.648758               1.0   \n",
       "9             0.063890  0.029260    0.239033  3.741299               1.0   \n",
       "8             0.122411  0.029260    0.457976  3.741299               1.0   \n",
       "11            0.122411  0.030685    0.428135  3.497518               1.0   \n",
       "10            0.071671  0.030685    0.250671  3.497518               1.0   \n",
       "28            0.059507  0.036055    0.190283  3.197672               1.0   \n",
       "29            0.189479  0.036055    0.605893  3.197672               1.0   \n",
       "34            0.120548  0.032000    0.367296  3.046884               1.0   \n",
       "37            0.087123  0.032000    0.265455  3.046884               1.0   \n",
       "\n",
       "    leverage  conviction  zhangs_metric   jaccard  certainty  kulczynski  \n",
       "14  0.023939    1.650122       0.944736  0.315453   0.393984    0.488122  \n",
       "15  0.023939    2.092205       0.929930  0.315453   0.522035    0.488122  \n",
       "9   0.021439    1.230158       0.834916  0.186322   0.187096    0.348505  \n",
       "8   0.021439    1.619096       0.782722  0.186322   0.382371    0.348505  \n",
       "11  0.021912    1.534608       0.769213  0.187793   0.348368    0.339403  \n",
       "10  0.021912    1.238881       0.813687  0.187793   0.192820    0.339403  \n",
       "28  0.024779    1.161509       0.847940  0.169326   0.139051    0.398088  \n",
       "29  0.024779    2.056601       0.730758  0.169326   0.513761    0.398088  \n",
       "34  0.021497    1.389989       0.735911  0.182158   0.280570    0.316375  \n",
       "37  0.021497    1.242778       0.763880  0.182158   0.195351    0.316375  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>representativity</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "      <th>jaccard</th>\n",
       "      <th>certainty</th>\n",
       "      <th>kulczynski</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>(Children)</td>\n",
       "      <td>(Animation)</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.048986</td>\n",
       "      <td>0.027068</td>\n",
       "      <td>0.423671</td>\n",
       "      <td>8.648758</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.023939</td>\n",
       "      <td>1.650122</td>\n",
       "      <td>0.944736</td>\n",
       "      <td>0.315453</td>\n",
       "      <td>0.393984</td>\n",
       "      <td>0.488122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>(Animation)</td>\n",
       "      <td>(Children)</td>\n",
       "      <td>0.048986</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.027068</td>\n",
       "      <td>0.552573</td>\n",
       "      <td>8.648758</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.023939</td>\n",
       "      <td>2.092205</td>\n",
       "      <td>0.929930</td>\n",
       "      <td>0.315453</td>\n",
       "      <td>0.522035</td>\n",
       "      <td>0.488122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>(Children)</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.029260</td>\n",
       "      <td>0.239033</td>\n",
       "      <td>3.741299</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021439</td>\n",
       "      <td>1.230158</td>\n",
       "      <td>0.834916</td>\n",
       "      <td>0.186322</td>\n",
       "      <td>0.187096</td>\n",
       "      <td>0.348505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(Children)</td>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>0.063890</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.029260</td>\n",
       "      <td>0.457976</td>\n",
       "      <td>3.741299</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021439</td>\n",
       "      <td>1.619096</td>\n",
       "      <td>0.782722</td>\n",
       "      <td>0.186322</td>\n",
       "      <td>0.382371</td>\n",
       "      <td>0.348505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(Fantasy)</td>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>0.071671</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.030685</td>\n",
       "      <td>0.428135</td>\n",
       "      <td>3.497518</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021912</td>\n",
       "      <td>1.534608</td>\n",
       "      <td>0.769213</td>\n",
       "      <td>0.187793</td>\n",
       "      <td>0.348368</td>\n",
       "      <td>0.339403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(Adventure)</td>\n",
       "      <td>(Fantasy)</td>\n",
       "      <td>0.122411</td>\n",
       "      <td>0.071671</td>\n",
       "      <td>0.030685</td>\n",
       "      <td>0.250671</td>\n",
       "      <td>3.497518</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021912</td>\n",
       "      <td>1.238881</td>\n",
       "      <td>0.813687</td>\n",
       "      <td>0.187793</td>\n",
       "      <td>0.192820</td>\n",
       "      <td>0.339403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>(Mystery)</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.059507</td>\n",
       "      <td>0.036055</td>\n",
       "      <td>0.190283</td>\n",
       "      <td>3.197672</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.024779</td>\n",
       "      <td>1.161509</td>\n",
       "      <td>0.847940</td>\n",
       "      <td>0.169326</td>\n",
       "      <td>0.139051</td>\n",
       "      <td>0.398088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>(Mystery)</td>\n",
       "      <td>(Thriller)</td>\n",
       "      <td>0.059507</td>\n",
       "      <td>0.189479</td>\n",
       "      <td>0.036055</td>\n",
       "      <td>0.605893</td>\n",
       "      <td>3.197672</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.024779</td>\n",
       "      <td>2.056601</td>\n",
       "      <td>0.730758</td>\n",
       "      <td>0.169326</td>\n",
       "      <td>0.513761</td>\n",
       "      <td>0.398088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>(Thriller, Drama)</td>\n",
       "      <td>(Crime)</td>\n",
       "      <td>0.087123</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.032000</td>\n",
       "      <td>0.367296</td>\n",
       "      <td>3.046884</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021497</td>\n",
       "      <td>1.389989</td>\n",
       "      <td>0.735911</td>\n",
       "      <td>0.182158</td>\n",
       "      <td>0.280570</td>\n",
       "      <td>0.316375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>(Crime)</td>\n",
       "      <td>(Thriller, Drama)</td>\n",
       "      <td>0.120548</td>\n",
       "      <td>0.087123</td>\n",
       "      <td>0.032000</td>\n",
       "      <td>0.265455</td>\n",
       "      <td>3.046884</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021497</td>\n",
       "      <td>1.242778</td>\n",
       "      <td>0.763880</td>\n",
       "      <td>0.182158</td>\n",
       "      <td>0.195351</td>\n",
       "      <td>0.316375</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "提升度最高的是 (Children, Animation) (儿童, 动画) ，二者具有强关联性。",
   "id": "58d01b672cf66e82"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T13:03:27.120369Z",
     "start_time": "2025-04-21T13:03:27.104962Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# .str.contains() 是pandas中Series的字符串方法，用于检查genres列中的每个元素是否包含子字符串\n",
    "# 筛选出genres列包含 'Children' 但(&)不包含(~) 'Animation' 的电影记录\n",
    "movies[(movies['genres'].str.contains('Children')) & (~movies['genres'].str.contains('Animation'))]"
   ],
   "id": "bf2bba606e94620e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      movieId                               title  \\\n",
       "1           2                      Jumanji (1995)   \n",
       "7           8                 Tom and Huck (1995)   \n",
       "26         27                 Now and Then (1995)   \n",
       "32         34                         Babe (1995)   \n",
       "36         38                 It Takes Two (1995)   \n",
       "...       ...                                 ...   \n",
       "8918   135268                    Zenon: Z3 (2004)   \n",
       "8960   139620  Everything's Gonna Be Great (1998)   \n",
       "8967   140152                 Dreamcatcher (2015)   \n",
       "8981   140747                    16 Wishes (2010)   \n",
       "9052   149354                      Sisters (2015)   \n",
       "\n",
       "                               genres  \n",
       "1          Adventure|Children|Fantasy  \n",
       "7                  Adventure|Children  \n",
       "26                     Children|Drama  \n",
       "32                     Children|Drama  \n",
       "36                    Children|Comedy  \n",
       "...                               ...  \n",
       "8918        Adventure|Children|Comedy  \n",
       "8960  Adventure|Children|Comedy|Drama  \n",
       "8967       Children|Crime|Documentary  \n",
       "8981           Children|Drama|Fantasy  \n",
       "9052                  Children|Comedy  \n",
       "\n",
       "[336 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movieId</th>\n",
       "      <th>title</th>\n",
       "      <th>genres</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jumanji (1995)</td>\n",
       "      <td>Adventure|Children|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>Tom and Huck (1995)</td>\n",
       "      <td>Adventure|Children</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>Now and Then (1995)</td>\n",
       "      <td>Children|Drama</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>34</td>\n",
       "      <td>Babe (1995)</td>\n",
       "      <td>Children|Drama</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>38</td>\n",
       "      <td>It Takes Two (1995)</td>\n",
       "      <td>Children|Comedy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8918</th>\n",
       "      <td>135268</td>\n",
       "      <td>Zenon: Z3 (2004)</td>\n",
       "      <td>Adventure|Children|Comedy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8960</th>\n",
       "      <td>139620</td>\n",
       "      <td>Everything's Gonna Be Great (1998)</td>\n",
       "      <td>Adventure|Children|Comedy|Drama</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8967</th>\n",
       "      <td>140152</td>\n",
       "      <td>Dreamcatcher (2015)</td>\n",
       "      <td>Children|Crime|Documentary</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8981</th>\n",
       "      <td>140747</td>\n",
       "      <td>16 Wishes (2010)</td>\n",
       "      <td>Children|Drama|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9052</th>\n",
       "      <td>149354</td>\n",
       "      <td>Sisters (2015)</td>\n",
       "      <td>Children|Comedy</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>336 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "具体分析还得落实到数据本身，这就需要充分理解数据才可以。",
   "id": "6ad1dac336037882"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
